package com.litchi.adapter;

import java.util.List;
import com.litchi.R;
import com.litchi.app.MyApplication;
import com.litchi.utils.Constants;
import com.litchi.utils.Utils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class GoodsListAdapter extends BaseAdapter {
	private MyApplication mApplication;
    private DisplayImageOptions options;
	private Context mContext;
	private List<com.litchi.bean.Goods> list;
	
	public GoodsListAdapter(Context mContext,List<com.litchi.bean.Goods> list){
		this.mContext=mContext;
		this.list=list;
		mApplication=(MyApplication)mContext.getApplicationContext();
		
		 options = new DisplayImageOptions.Builder().bitmapConfig(Bitmap.Config.RGB_565)
		            // •在DisplayImageOptions选项中配置bitmapConfig为Bitmap.Config.RGB_565，因为默认是ARGB_8888，
		            // 使用RGB_565会比使用ARGB_8888少消耗2倍的内存
		            .showImageOnLoading(R.drawable.default_picture)
		            .showImageForEmptyUri(R.drawable.default_picture)
		            .showImageOnFail(R.drawable.default_picture)
		            .cacheInMemory(true)
		            .cacheOnDisc(true)
		            .considerExifParams(false)
		            .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
		            .displayer(new RoundedBitmapDisplayer(Utils.convertDipOrPx(mContext, 3)))
		            .build();
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHold mViewHold  ;
		if (convertView==null) {
			mViewHold=new ViewHold();
			convertView=LayoutInflater.from(mContext).inflate(R.layout.goods_item, null);
			mViewHold.goodsImageView=(ImageView)convertView.findViewById(R.id.goods_img);
			mViewHold.desTextView=(TextView)convertView.findViewById(R.id.des_goods);
			mViewHold.priceTextView=(TextView)convertView.findViewById(R.id.price_goods);
			convertView.setTag(mViewHold);
			
		}else{
			mViewHold=(ViewHold)convertView.getTag();
		}
		mViewHold.desTextView.setText(list.get(position).getGoodsName());
		mViewHold.priceTextView.setText(list.get(position).getGoodsPrice());
        mApplication.imgLoader.displayImage(Constants.HTTP_FILE+list.get(position).getGoodsImg().getUrl(), mViewHold.goodsImageView );
		
		return convertView;
	}
	
	
	class ViewHold {
		private ImageView goodsImageView;
		private TextView desTextView;
		private TextView priceTextView;
		
	}
	
	 
}